﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevFx.Web.Code;
using DevFx.Model;
using DevFx.Biz;

namespace DevFx.Web.Pages.Rights
{
    public partial class RoleMenuList : BasePage
    {
        #region 变量

        private MenuBiz m = new MenuBiz();
        private vRoleMenuBiz vm = new vRoleMenuBiz();
        private RoleMenuBiz rm = new RoleMenuBiz();
        private RoleBiz r = new RoleBiz();
        #endregion

        #region 属性
        /// <summary>
        /// ID
        /// </summary>
        public Guid RID
        {
            get
            {

                if (ViewState["RID"] == null)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                    {
                        ViewState["RID"] = Request.QueryString["id"].ToString();
                    }
                    else
                    {
                        ViewState["RID"] = Guid.Empty;
                    }
                }
                Guid g = new Guid(ViewState["RID"].ToString());
                return g;
            }
            set { ViewState["RID"] = value; }
        }
        #endregion

        #region 方法
        private void BindData()
        {
            var role = r.GetByCondition(p => p.RoleID == RID);
            this.lblName.Text = role.RoleName;
            this.lblID.Text = role.RoleID.ToString();
            this.tvList.Nodes.Clear();
            var list = m.GetList();
            TreeNode node = new TreeNode();
            node.Text = "菜单列表";
            node.Value = Guid.Empty.ToString();
            var checkList = vm.GetList(p => p.RoleID.Value == RID);
            GetNodeList(list, Guid.Empty, node, checkList);
            this.tvList.Nodes.Add(node);
            node.ExpandAll();
        }
        private void GetNodeList(List<SysMenu> List, Guid ParentID, TreeNode node, List<vSysRoleMenu> checkList)
        {
            List<SysMenu> list = List.FindAll(delegate(SysMenu m) { return m.ParentID == ParentID; });
            foreach (var b in list)
            {
                TreeNode NewNodes = new TreeNode();
                NewNodes.Text = b.Name;
                NewNodes.ShowCheckBox = true;
                //判断这个菜单是否已经选上
                if (checkList.FindAll(p => p.MenuID == b.MenuID).Count() > 0)
                {
                    NewNodes.Checked = true;
                }
                NewNodes.Value = b.MenuID.ToString();
                GetNodeList(List, b.MenuID, NewNodes, checkList);
                node.ChildNodes.Add(NewNodes);
            }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        //全选
        protected void btnCheckAll_Click(object sender, EventArgs e)
        {
            checkdAllTree(tvList.Nodes);
        }
        //反选
        protected void btnCheckOther_Click(object sender, EventArgs e)
        {
            checkdOther(tvList.Nodes);
        }
        //全选
        private void checkdAllTree(TreeNodeCollection trees)
        {
            foreach (TreeNode node in trees)
            {
                if (node.Checked) { } else { node.Checked = true; };
                if (node.ChildNodes.Count != 0)
                    checkdAllTree(node.ChildNodes);
            }
        }
        //反选
        private void checkdOther(TreeNodeCollection trees)
        {
            foreach (TreeNode node in trees)
            {

                if (node.Checked) { node.Checked = false; } else { node.Checked = true; };

                if (node.ChildNodes.Count != 0)
                    checkdOther(node.ChildNodes);
            }
        }

        //保存菜单信息
        protected void tvList_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            var node = e.Node;
            if (node.Checked)
            {
                //判断是否已经存在
                Guid mID = new Guid(node.Value);
                if (!(rm.GetList(p => p.RoleID == RID && p.MenuID == mID).Count() > 0))
                {
                    var model = new SysRoleMenu();
                    model.RoleMenuID = new Guid();
                    model.RoleID = RID;
                    model.MenuID = mID;
                    model.ModifyBy = model.CreateBy = UserID;
                    model.CreateDate = model.ModifyDate = DateTime.Now;
                    rm.Create(model);
                }

            }
            else
            {
                //判断是否存在
                Guid mID = new Guid(node.Value);
                var list = rm.GetList(p => p.RoleID == RID && p.MenuID == mID);
                if (list.Count() > 0)
                {
                    rm.Delete(list);
                }
            }
        }
        #endregion
    }
}